package com.itheima.demo4;

import com.itheima.demo2.Student;

import java.lang.reflect.Field;

public class Demo {
        public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
            Student student = new Student("张三",25);
            Object name = getProperty(student, "name","李四");
            System.out.println(name);
        }

        public static Object getProperty(Object obj,String propertyName,Object value) throws NoSuchFieldException, IllegalAccessException {
            Class aClass = obj.getClass();
            Field declaredField = aClass.getDeclaredField(propertyName);
            declaredField.setAccessible(true);
            declaredField.set(obj,value);
            return declaredField.get(obj);
        }

}
